package com.atguigu.serviceedu.controller;


import com.atguigu.commonutils.R;
import com.atguigu.servicebase.exceptionhandler.GuLiException;
import com.atguigu.serviceedu.entity.EduTeacher;
import com.atguigu.serviceedu.entity.vo.TeacherQuery;
import com.atguigu.serviceedu.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author jyd
 * @since 2022-03-11
 */
@Api(description="讲师管理")
@RestController
//@CrossOrigin
@RequestMapping("/eduservice/teacher")//localhost:8001/serviceedu/teacher/findAll
public class EduTeacherController {
    //创建service对象
    @Autowired
    private EduTeacherService service;
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("findAll")
    public R findAllTeacher(){
        List<EduTeacher> teacherList = service.list(null);
        return R.ok().data("items",teacherList);
    }
    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("{id}")
    public R removeById(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable String id){
        boolean flag = service.removeById(id);
        return flag ? R.ok():R.error();
    }
    @ApiOperation(value = "讲师分页")
    @GetMapping("PageTeacher/{current}/{limit}")
    public R PageListTeacher(@PathVariable Long current ,@PathVariable Long limit){
        Page<EduTeacher> teacherPage = new Page<>(current,limit);

//        try{
//            int i = 10/0;
//        }catch (Exception e){
//            throw new GuLiException(20001,"执行了自定义异常");
//        }

        service.page(teacherPage,null);
        long total = teacherPage.getTotal();
        List<EduTeacher> teacherList = teacherPage.getRecords();
        return R.ok().data("total",total).data("roes",teacherList);
    }
    @ApiOperation(value = "条件查询讲师分页")
    @PostMapping("PageTeacherCondition/{current}/{limit}")
    public R PageTeacherCondition(@PathVariable Long current, @PathVariable Long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery){
        Page<EduTeacher> teacherPage = new Page<>(current,limit);
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)){
            wrapper.le("gmt_create",end);
        }
        wrapper.orderByDesc("gmt_create");
        service.page(teacherPage,wrapper);
        long total = teacherPage.getTotal();
        List<EduTeacher> teacherList = teacherPage.getRecords();
        return R.ok().data("total",total).data("rows",teacherList);
    }
    //添加讲师方法
    @ApiOperation(value = "添加讲师功能")
    @PostMapping("AddTeacher")
    public R AddTeacher(@RequestBody EduTeacher eduTeacher){
        boolean save = service.save(eduTeacher);
        if(save){
            return R.ok();
        }else{
            return R.error();
        }
    }
    @ApiOperation(value = "根据id查询讲师数据")
    @GetMapping("GetTeacher/{id}")
    public R SelectTeacherById(@PathVariable String id){
        EduTeacher eduTeacher = service.getById(id);
        return R.ok().data("teacher",eduTeacher);
    }
    @ApiOperation(value = "修改讲师")
    @PostMapping("UpdateTeacher")
    public R UpdateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean flag = service.updateById(eduTeacher);
        return flag ? R.ok() : R.error();
    }
    //查询教师教师名称
//    @ApiOperation(value = "查询讲师名称")
//    @GetMapping("getTeacher")
//    public R getTeacher(){
//        List<EduTeacher> teacherList = service.list(null);
//        List<String> teacherNames = new ArrayList<>();
//        for (EduTeacher eduTeacher : teacherList) {
//            String name = eduTeacher.getName();
//            teacherNames.add(name);
//        }
//        return R.ok().data("list",teacherNames);
//    }
}

